package com.atguigu.acl.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.atguigu.acl.entity.Admin;
import com.atguigu.acl.entity.AdminRole;
import com.atguigu.acl.entity.Resource;
import com.atguigu.acl.mapper.AdminRoleMapper;
import com.atguigu.acl.service.AdminCacheService;
import com.atguigu.acl.service.AdminService;
import com.atguigu.serviceBase.service.RedisService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Summerday
 */
@Service
public class AdminCacheServiceImpl implements AdminCacheService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Value("${redis.database}")
    private String REDIS_DATABASE;
    @Value("${redis.expire.common}")
    private Long REDIS_EXPIRE;
    @Value("${redis.key.admin}")
    private String REDIS_KEY_ADMIN;
    @Value("${redis.key.resourceList}")
    private String REDIS_KEY_RESOURCE_LIST;

    @Override
    public void delAdmin(Long adminId) {
        Admin admin = adminService.getById(adminId);
        if (admin != null) {
            String key = REDIS_DATABASE + ":" + REDIS_KEY_ADMIN + ":" + admin.getUsername();
            redisService.del(key);
        }
    }

    @Override
    public void delResourceList(Long adminId) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_RESOURCE_LIST + ":" + adminId;
        redisService.del(key);
    }

    @Override
    public void delResourceListByRole(Long roleId) {

        QueryWrapper<AdminRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        List<AdminRole> list = adminRoleMapper.selectList(wrapper);
        if (CollUtil.isNotEmpty(list)) {
            String keyPrefix = REDIS_DATABASE + ":" + REDIS_KEY_RESOURCE_LIST + ":";
            //得到所有需要删除的keys
            List<String> keys = list.stream().map(item -> keyPrefix + item.getAdminId()).collect(Collectors.toList());
            redisService.del(keys);
        }
    }

    @Override
    public void delResourceListByRoleIds(List<Long> roleIds) {
        QueryWrapper<AdminRole> wrapper = new QueryWrapper<>();
        wrapper.in("role_id", roleIds);
        List<AdminRole> list = adminRoleMapper.selectList(wrapper);
        if (CollUtil.isNotEmpty(list)) {
            String keyPrefix = REDIS_DATABASE + ":" + REDIS_KEY_RESOURCE_LIST + ":";
            //得到所有需要删除的keys
            List<String> keys = list.stream().map(item -> keyPrefix + item.getAdminId()).collect(Collectors.toList());
            redisService.del(keys);
        }

    }

    @Override
    public void delResourceListByResource(Long resourceId) {
        //获取资源相关的用户id
        List<Long> adminList = adminRoleMapper.getAdminList(resourceId);

        if (CollUtil.isNotEmpty(adminList)) {
            String keyPrefix = REDIS_DATABASE + ":" + REDIS_KEY_RESOURCE_LIST + ":";
            //得到所有需要删除的keys
            List<String> keys = adminList.stream().map(adminId -> keyPrefix + adminId).collect(Collectors.toList());
            redisService.del(keys);
        }


    }

    @Override
    public Admin getAdmin(String username) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_ADMIN + ":" + username;
        return (Admin) redisService.get(key);
    }

    @Override
    public void setAdmin(Admin admin) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_ADMIN + ":" + admin.getUsername();
        redisService.set(key, admin, REDIS_EXPIRE);
    }

    @Override
    public List<Resource> getResourceList(Long adminId) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_RESOURCE_LIST + ":" + adminId;
        return (List<Resource>) redisService.get(key);
    }

    @Override
    public void setResourceList(Long adminId, List<Resource> resourceList) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_RESOURCE_LIST + ":" + adminId;
        redisService.set(key, resourceList, REDIS_EXPIRE);
    }
}
